# -*- coding: utf-8 -*-
"""
-------------------------------------------------
# @Project  :pythonProject
# @File     :face
# @Date     :2021/10/22 14:20
# @Author   :zhumenglong
# @Software :PyCharm
-------------------------------------------------
"""
from wareHouse import *

class Face:
    def __init__(self, file_path, Index_path, goal_path):
        self.file_path = file_path
        self.Index_path = Index_path
        self.goal_path = goal_path

    def get_keypoint(self):
        with open(self.Index_path, 'r', encoding='utf-8') as fp :
            while True:
                file_name = fp.readline()[:-1]
                if len(file_name) > 0:
                    json_name = self.file_path + file_name + '_info.json'
                    pict_name = self.file_path + file_name + '.jpg'
                    Face.draw_pic_jpg(self, file_name, json_name, pict_name)

                else:
                    break

    def draw_pic_jpg(self, file_name, json_name, pict_name):
        with open(json_name, 'rb') as f:
            json_info = json.loads(f.read())
            img_info = method.cv2_imread(pict_name, -1)
            for i in json_info:
                if i == "face":
                    cat_name = "ped_box"
                    for num in json_info[i]:
                        for j in num:
                            if j == "box":
                                xmin = int(num[j]['x'])
                                ymin = int(num[j]['y'])
                                xmax = int(num[j]['x']) + int(num[j]['width'])
                                ymax = int(num[j]['y']) + int(num[j]['height'])

                                x = 0
                                y = 250
                                z = 0
                                color = (x, y, z)
                                cv2.rectangle(img_info, (int(xmin), int(ymin)), (int(xmax), int(ymax)), color, 2)
                                font = cv2.FONT_HERSHEY_SIMPLEX
                                cv2.putText(img_info, cat_name, (int(xmin), int(ymin)), font, 1, color, 2)
                            elif j == "glass":
                                if num[j]["value"] == 1:
                                    cv2.putText(img_info,"no glass",(1000,200),font,1,(250,0,0),2)
                                elif num[j]["value"] == 2:
                                    cv2.putText(img_info, "have glass", (1000, 200), font, 1, (250, 0, 0), 2)
                                else:
                                    cv2.putText(img_info, "sun glass", (1000, 200), font, 1, (250, 0, 0), 2)
                            elif j == "hat":
                                if num[j]["value"] == 1:
                                    cv2.putText(img_info, "no hat", (1000, 250), font, 1, (250, 0, 0), 2)
                                elif num[j]["value"] == 2:
                                    cv2.putText(img_info, "have hat", (1000, 250), font, 1, (250, 0, 0), 2)
                                else:
                                    cv2.putText(img_info, "have toujin", (1000, 250), font, 1, (250, 0, 0), 2)
                            elif j == "mask":
                                if num[j]["value"] == 1:
                                    cv2.putText(img_info, "no mask", (1000, 300), font, 1, (250, 0, 0), 2)
                                else:
                                    cv2.putText(img_info, "have mask", (1000, 300), font, 1, (250, 0, 0), 2)
                            else:
                                continue

                            if num["glass"]["value"] >= 2 or num["hat"]["value"] >= 2 or num["mask"]["value"] >= 2 :
                                cv2.putText(img_info, "Error",(1000,100),font,2,(240,250,0),2)

            method.create_path(self.goal_path)

            method.cv2_imwrite_jpg("./" + self.goal_path + "/" + file_name + ".jpg", img_info)
